﻿using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls;
using DGP.Framework.Web.UI;
using Model;

public partial class Category : BasePage
{
    protected Categories current;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
        {
            current = Entities.GetCategoryDetails(Convert.ToInt32(Request.QueryString["ID"]));
            litNav.Text = LoadNavigation(current);
            litTitle.Text = current.Title;
            litContent.Text = current.Description;
            if (!string.IsNullOrEmpty(current.ImagePath))
            {
                var img =
                    new Bitmap(Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_PHYSICAL_PATH) +
                               current.ImagePath);
                imgCateg.ImageUrl = Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_URL_PREFIX) +
                                    current.ImagePath;
                if (img.Width > img.Height)
                    imgCateg.Width = new Unit(250);
                else
                    imgCateg.Height = new Unit(250);
            }
            else
                imgCateg.ImageUrl = Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_URL_PREFIX) +
                                    Constants.NO_IMAGE_FILE_NAME;
            imgCateg.AlternateText = current.Title;

            dlSubCategories.DataSource = Entities.GetCategories(current.CategoryID);
            dlSubCategories.DataBind();

            repAllArticles.DataSource = Entities.GetArticlesByCategory(current.CategoryID);
            repAllArticles.DataBind();

            // set title and meta tags;
            Title = current.Title;
            Meta_Description = current.MetaDesc;
            Meta_Keywords = current.MetaKeys;
        }
        else
        {
            litMessage.Text = "Aceasta categorie nu exista";
        }
    }

    protected string LoadNavigation(Categories categ)
    {
        var sb = new StringBuilder();
        sb.Append(@"<a href=""default.aspx"">Prima pagina</a>");

        Categories currentCategory = categ;
        var nav = new ArrayList {currentCategory};
        while (currentCategory.Parent.CategoryID != 0)
        {
            try
            {
                currentCategory = currentCategory.Parent;
                nav.Add(currentCategory);
            }
            catch
            {
                break;
            }
        }
        for (int i = nav.Count - 1; i >= 0; i--)
        {
            var x = (Categories) nav[i];
            if (categ.CategoryID != Convert.ToInt32(x.CategoryID))
                sb.Append(@" » <a href=""" + Utils.BuildURL(PageType.Categorii, x.CategoryID, x.Title) + @""">" +
                          x.Title + @"</a>");
            else
                sb.Append(@" » " + x.Title);
        }
        return sb.ToString();
    }

    protected void dlSubCategories_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        var subsubCategories = (Repeater) e.Item.FindControl("subsubCategories");
        var dataSource = Entities.GetCategories(((Categories) e.Item.DataItem).CategoryID);
        if (dataSource.Length > 0)
        {
            subsubCategories.Visible = true;
            subsubCategories.DataSource = dataSource;
            subsubCategories.DataBind();
        }
        //bind articles
        //if (subsubCategories.Items.Count == 0)
        {
            var repArticles = (Repeater) e.Item.FindControl("repArticles");
            var articlesByCategory = Entities.GetArticlesByCategory(((Categories) e.Item.DataItem).CategoryID);
            if (articlesByCategory != null && articlesByCategory.Length > 0)
            {
                repArticles.Visible = true;
                repArticles.DataSource = articlesByCategory;
                repArticles.DataBind();
            }
        }
    }
}